import AdsServiceExtensionAbility from '@ohos.advertising.AdsServiceExtensionAbility';
import { RespCallback } from '@ohos.advertising.AdsServiceExtensionAbility';
import advertising from '@ohos.advertising';
import Logger from '../adsmanager/log/HiAdLog';

const TAG: string = "AdsExtensionAbility";

export default class AdsExtensionAbility extends AdsServiceExtensionAbility {
  /**
   * 请求广告接口，接收媒体广告请求
   *
   * @param adParam 客户端发送的广告请求参数。
   * @param adOptions 客户端发送的请求广告的广告选项。
   * @param respCallback 广告请求结果回调方法。
   * @returns
   */
  onLoadAd(adParam: advertising.AdRequestParams, adOptions: advertising.AdOptions, respCallback: RespCallback) {
    Logger.i(TAG, `onLoadAd, adParam: ${JSON.stringify(adParam)}, adOptions: ${JSON.stringify(adOptions)}`);
    const adType: number = adParam.adType;
    const ads: Array<advertising.Advertisement> = [];
    // 构造返回给客户端的广告数据
    ads.push({
      adType: adType,
      uniqueId: '122222',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    ads.push({
      adType: adType,
      uniqueId: '123333',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    ads.push({
      adType: adType,
      uniqueId: '123444',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    const slot1: string = "slot1";
    const resMap: Map<string, Array<advertising.Advertisement>> = new Map();
    resMap.set(slot1, ads);
    Logger.i(TAG, `onLoadAd, resMap: ${JSON.stringify(resMap.get(slot1))}`);
    respCallback(resMap);
  }

  /**
   * 请求广告接口，接收媒体多广告位广告请求
   *
   * @param adParams 客户端发送的广告请求参数数组。
   * @param adOptions 客户端发送的请求广告的广告选项。
   * @param respCallback 广告请求结果回调方法。
   * @returns
   */
  onLoadAdWithMultiSlots(adParams: advertising.AdRequestParams[], adOptions: advertising.AdOptions, respCallback: RespCallback) {
    Logger.i(TAG, `onLoadAdWithMultiSlots, adParams: ${JSON.stringify(adParams)}, adOptions: ${JSON.stringify(adOptions)}`);
    const adType: number = adParams[0].adType;
    const ads: Array<advertising.Advertisement> = [];
    // 构造返回给客户端的广告数据
    ads.push({
      adType: adType,
      uniqueId: '122222',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    ads.push({
      adType: adType,
      uniqueId: '123333',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    const slot1: string = "slot1";
    const adType2: number = adParams[1].adType;
    const ads2: Array<advertising.Advertisement> = [];
    ads2.push({
      adType: adType2,
      uniqueId: '122222',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    ads2.push({
      adType: adType2,
      uniqueId: '123333',
      rewardVerifyConfig: null,
      rewarded: false,
      shown: false,
      clicked: false
    });
    const slot2: string = "slot2";
    const resMap: Map<string, Array<advertising.Advertisement>> = new Map();
    resMap.set(slot1, ads);
    resMap.set(slot2, ads2);
    Logger.i(TAG, `onLoadAdWithMultiSlots, slot1: ${JSON.stringify(resMap.get(slot1))}`);
    Logger.i(TAG, `onLoadAdWithMultiSlots, slot2: ${JSON.stringify(resMap.get(slot2))}`);
    respCallback(resMap);
  }
}